package com.xinggq.user.service.dto.tenant;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.xinggq.user.service.entity.SysTenantEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 租户信息VO
 *
 * @author xinggq
 * @since 2025-01-01
 */
@Data
@Schema(description = "租户信息")
public class TenantVO {

    @Schema(description = "租户ID")
    private Long id;

    @Schema(description = "租户编码")
    private String tenantCode;

    @Schema(description = "租户名称")
    private String tenantName;

    @Schema(description = "租户域名")
    private String domain;

    @Schema(description = "联系人")
    private String contactPerson;

    @Schema(description = "联系电话")
    private String contactPhone;

    @Schema(description = "联系邮箱")
    private String contactEmail;

    @Schema(description = "租户地址")
    private String address;

    @Schema(description = "租户描述")
    private String description;

    @Schema(description = "租户状态：1-正常，0-禁用")
    private Integer status;

    @Schema(description = "状态描述")
    private String statusDesc;

    @Schema(description = "到期时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime expireTime;

    @Schema(description = "最大用户数")
    private Integer maxUsers;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "创建人ID")
    private Long createBy;

    @Schema(description = "更新人ID")
    private Long updateBy;

    /**
     * 获取状态描述
     */
    public String getStatusDesc() {
        if (status == null) {
            return "未知";
        }
        return status == 1 ? "正常" : "禁用";
    }

    /**
     * 判断是否已过期
     */
    public boolean isExpired() {
        return expireTime != null && expireTime.isBefore(LocalDateTime.now());
    }

    public static TenantVO fromEntity(SysTenantEntity entity) {
        if (entity == null) {
            return null;
        }
        TenantVO vo = new TenantVO();
        vo.setId(entity.getId());
        vo.setTenantCode(entity.getTenantCode());
        vo.setTenantName(entity.getTenantName());
        vo.setDomain(entity.getDomain());
        vo.setContactPerson(entity.getContactPerson());
        vo.setContactPhone(entity.getContactPhone());
        vo.setContactEmail(entity.getContactEmail());
        vo.setStatus(entity.getStatus());
        vo.setExpireTime(entity.getExpireTime());
        vo.setMaxUsers(entity.getMaxUsers());
        vo.setCreateTime(entity.getCreatedAt());
        vo.setCreateBy(entity.getCreatedBy());
        return vo;
    }
}